using System;
using System.IO;
using System.Text;

namespace Microsoft.EPM.Events
{
    class RolesDataFile
    {
        #region Properties

        private StreamWriter swData;
        private StreamReader swReaderData;

        private string fileName;

        /// <summary>
        /// Stream writer
        /// </summary>
        public StreamWriter SwData
        {
            get { return swData; }
        }

        /// <summary>
        /// File name
        /// </summary>
        public String FileName
        {
            get { return fileName; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Create Data file
        /// </summary>
        /// rolesFiles
        /// logFolder
        public RolesDataFile (string rolesFile, string DataFolder, bool Reader)
        {
            fileName = string.Format("OLAPRoles_for_{0}.txt", rolesFile);

            string path = DataFolder + fileName;
            if (Reader)
            {
                swReaderData = new StreamReader(path);
            }
            else
            {
                try
                {
                    swData = new StreamWriter(path);
                }
                catch (Exception)
                {
                    System.Threading.Thread.Sleep(1000);
                    swData = new StreamWriter(Environment.CurrentDirectory + "\\" + string.Format("OLAPRoles_for_{0}.txt", rolesFile));
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Read entry from Data file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        public string ReadLine()
        {
           return  swReaderData.ReadLine();
        }

        /// <summary>
        /// Write entry to Data file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        public void WriteLine(string toWrite)
        {
            WriteLine(toWrite, false);
        }

        /// <summary>
        /// Write entry to Data file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        /// <param name="dateTime">write date time?</param>
        public void WriteLine(string toWrite, bool dateTime)
        {
            if (dateTime)
                swData.WriteLine(string.Format("{0} {1}", DateTime.Now.ToLongTimeString(), toWrite));
            else
                swData.WriteLine(toWrite);
        }

        /// <summary>
        /// Write entry to Data file
        /// </summary>
        /// <param name="sw">stream writer</param>
        /// <param name="toWrite">entry to write</param>
        public static void WriteLine(StreamWriter sw, string toWrite)
        {
            sw.WriteLine(DateTime.Now.ToLongTimeString() + " : " + toWrite);
        }

        /// <summary>
        /// Close stream writer
        /// </summary>
        public void CloseWriter()
        {
            if (swData != null)
            {
                swData.Close();
            }
        }

        /// <summary>
        /// Close stream Reader
        /// </summary>
        public void CloseReader()
        {
            if (swReaderData != null)
            {
                swReaderData.Close();
            }
        }
        #endregion
    }
}
